home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / hscprj / readprj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  11.7 KB  |  450 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1995-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * hscprj/readprj.c
  22.  *
  23.  * project managment input-routines for hsc
  24.  *
  25.  * updated: 10-Sep-1996
  26.  * created: 10-Sep-1996
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <time.h>
  34.  
  35. #include "hsclib/ldebug.h"
  36. #include "hscprj/pdebug.h"
  37. #include "hscprj/pdefs.h"
  38.  
  39. #include "ugly/utypes.h"
  40. #include "ugly/dllist.h"
  41. #include "ugly/expstr.h"
  42. #include "ugly/umemory.h"
  43. #include "ugly/infile.h"
  44. #include "ugly/ustring.h"
  45.  
  46. #include "hscprj/document.h"
  47. #include "hscprj/project.h"
  48.  
  49. /*
  50.  * hsc_msg_project_corrupt: display error message about
  51.  * corrupt project file
  52.  */
  53. static VOID hsc_msg_project_corrupt(HSCPRJ * hp, STRPTR descr)
  54. {
  55.     hp->fatal = TRUE;
  56.     if (hp->CB_msg_corrupt_pf)
  57.         (*(hp->CB_msg_corrupt_pf)) (hp, descr);
  58. }
  59.  
  60. /* convert hex-digit to int */
  61. static int x2int(char c)
  62. {
  63.     if ((c >= '0') && (c <= '9'))
  64.         return (c - '0');
  65.     if ((c >= 'A') && (c <= 'F'))
  66.         return (c - 'A' + 10);
  67.     if ((c >= 'a') && (c <= 'f'))
  68.         return (c - 'a' + 10);
  69.     return (EOF);
  70. }
  71.  
  72. /*
  73.  * read_long: read (unsigned) long integer value
  74.  */
  75. static ULONG read_ulong(HSCPRJ * hp)
  76. {
  77.     INFILE *inpf = hp->inpf;
  78.     ULONG num = 0;
  79.     int ch;
  80.     int digit = EOF;
  81.  
  82.     do
  83.     {
  84.         ch = infgetc(inpf);
  85.         if (ch != ' ')
  86.         {
  87.             digit = x2int(ch);
  88.             if (digit == EOF)
  89.                 num = 0;
  90.             else
  91.                 num = (num << 4) + digit;
  92.         }
  93.         if (digit == EOF)
  94.             hsc_msg_project_corrupt(hp, "hex digit expected");
  95.     }
  96.     while ((digit != EOF) && (ch != ' '));
  97.  
  98.     if (digit == EOF)
  99.         num = 0;
  100.  
  101.     return (num);
  102. }
  103.  
  104. /*
  105.  * read_string:
  106.  *
  107.  * read string length, alloc mem and read string into it
  108.  */
  109. static STRPTR read_string(HSCPRJ * hp)
  110. {
  111.     STRPTR dest = NULL;
  112.  
  113.     ULONG len = read_ulong(hp);
  114.  
  115.     if (len)
  116.     {
  117.         ULONG i;
  118.         int ch = 'x';           /* dummy init */
  119.  
  120.         /* alloc mem */
  121.         dest = umalloc((size_t) (len + 1));
  122.         dest[len] = '\0';
  123.  
  124.         for (i = 0; ((i < len) && (ch != EOF)); i++)
  125.         {
  126.             ch = infgetc(hp->inpf);
  127.             if (ch != EOF)
  128.                 dest[i] = ch;
  129.             else
  130.                 hsc_msg_project_corrupt(hp, "string expected");
  131.         }
  132.         if (ch != EOF)
  133.             dest[len] = 0;
  134.         else
  135.         {
  136.             ufree(dest);
  137.             dest = NULL;
  138.         }
  139.     }
  140.     return (dest);
  141. }
  142.  
  143. /*
  144.  * read_caller: read file position
  145.  */
  146. static CALLER *read_caller(HSCPRJ * hp)
  147. {
  148.     CALLER *caller = NULL;
  149.     STRPTR callerid = infgetw(hp->inpf);
  150.  
  151.     if (callerid && !upstrcmp(callerid, ID_CALLER_STR))
  152.     {
  153.         int ch = infgetc(hp->inpf);     /* skip blank */
  154.  
  155.         if (ch == ' ')
  156.         {
  157.             STRPTR fname = read_string(hp);
  158.             caller = new_caller(fname, read_ulong(hp), read_ulong(hp));
  159.             ufreestr(fname);
  160.         }
  161.         else
  162.         {
  163.             hsc_msg_project_corrupt(hp, "blank expected");
  164.         }
  165.     }
  166.     else if (callerid && !strcmp(callerid, "\n"))
  167.     {
  168.         /* skip empty caller */
  169.         inungetcw(hp->inpf);
  170.         D(fprintf(stderr, DHP "skip EMPTY CALLER\n"));
  171.     }
  172.     else
  173.     {
  174.         hsc_msg_project_corrupt(hp, ID_CALLER_STR " expected");
  175.     }
  176.     return (caller);
  177. }
  178.  
  179. /*
  180.  * read_lf: read linefeed
  181.  */
  182. static BOOL read_lf(HSCPRJ * hp)
  183. {
  184.     int ch = infgetc(hp->inpf);
  185.     BOOL ok = TRUE;
  186.  
  187.     if (ch != '\n')
  188.     {
  189.         hsc_msg_project_corrupt(hp, "linefeed expected");
  190.         ok = FALSE;
  191.     }
  192.     return (ok);
  193. }
  194.  
  195. /*
  196.  * read command
  197.  *
  198.  * returns next command or NULL if eof reached
  199.  */
  200. static STRPTR read_command(HSCPRJ * hp)
  201. {
  202.     STRPTR command;
  203.  
  204.     do
  205.     {
  206.         command = infgetw(hp->inpf);
  207.     }
  208.     while (command && !strcmp(command, "\n"));
  209.  
  210.     if (command)
  211.     {
  212.         /* skip blanks */
  213.         infskip_ws(hp->inpf);
  214.         DP(fprintf(stderr, DHP "command `%s'\n", command));
  215.     }
  216.     else
  217.     {
  218.         DP(fprintf(stderr, DHP "command EOF\n"));
  219.     }
  220.  
  221.     return (command);
  222. }
  223.  
  224. /*
  225.  * read header
  226.  */
  227. static BOOL read_header(HSCPRJ * hp)
  228. {
  229.     STRARR fileid[1 + sizeof(FILEID_HSCPRJ)];
  230.     BOOL ok = FALSE;
  231.     STRPTR cmd = NULL;
  232.     size_t i;
  233.  
  234.     /* read fileid */
  235.     for (i = 0; i < strlen(FILEID_HSCPRJ); i++)
  236.     {
  237.         int ch = infgetc(hp->inpf);
  238.  
  239.         fileid[i] = (UBYTE) ch;
  240.     }
  241.     fileid[i] = '\0';
  242.  
  243.     DP(fprintf(stderr, DHP "fileid: `%s'\n", fileid));
  244.  
  245.     /* check fileid */
  246.     if (!strcmp(fileid, FILEID_HSCPRJ))
  247.     {
  248.         DP(fprintf(stderr, DHP "fileid: `%s'\n", fileid));
  249.         ok = read_lf(hp);
  250.     }
  251.     else
  252.     {
  253.         hsc_msg_project_corrupt(hp, "wrong file-id");
  254.     }
  255.  
  256.     if (ok)
  257.     {
  258.  
  259.         ok = FALSE;
  260.  
  261.         /* read version */
  262.         cmd = read_command(hp);
  263.  
  264.         /* check version */
  265.         if (cmd && !strcmp(cmd, LINE_VERSION_STR))
  266.         {
  267.             ULONG version = read_ulong(hp);
  268.  
  269.             DP(fprintf(stderr, DHP "version: %lu\n", version));
  270.  
  271.             if (version && (version <= 2))
  272.             {
  273.                 ok = read_lf(hp);
  274.             }
  275.             else
  276.             {
  277.                 hsc_msg_project_corrupt(hp, "wrong version");
  278.             }
  279.  
  280.         }
  281.         else
  282.         {
  283.             hsc_msg_project_corrupt(hp, "unknown version");
  284.         }
  285.  
  286.     }
  287.     return (ok);
  288. }
  289.  
  290. /*
  291.  * hsc_project_read_data
  292.  *
  293.  * read project file
  294.  *
  295.  * params: hp....HSCPRJ created using new_project()
  296.  *         inpf..input file opened using infopen();
  297.  *               this has to be opened and closed by
  298.  *               you outside this function.
  299.  */
  300. BOOL hsc_project_read_data(HSCPRJ * hp, INFILE * inpf)
  301. {
  302.     BOOL ok = FALSE;
  303.  
  304.     if (inpf)
  305.     {
  306.         hp->inpf = inpf;
  307.  
  308.         DP(fprintf(stderr, DHP "read project-file from `%s'\n",
  309.                      infget_fname(inpf)));
  310.  
  311.         if (read_header(hp))
  312.         {
  313.             HSCDOC *document = NULL;
  314.             STRPTR cmd = NULL;
  315.  
  316.             do
  317.             {
  318.                 cmd = read_command(hp);
  319.                 if (cmd)
  320.                 {
  321.                     if (!strcmp(cmd, LINE_REM_STR))
  322.                     {
  323.                         /* skip comment */
  324.                         int ch;
  325.                         DP(fprintf(stderr, DHP "comment `"));
  326.                         do
  327.                         {
  328.                             ch = infgetc(inpf);
  329.                             DP(
  330.                                     {
  331.                                     if (ch != '\n')
  332.                                     fprintf(stderr, "%c", ch);
  333.                                     }
  334.                             );
  335.                         }
  336.                         while ((ch != EOF) && (ch != '\n'));
  337.                         DP(fprintf(stderr, "'\n"));
  338.                     }
  339.                     else if (!strcmp(cmd, LINE_DOCUMENT_STR))
  340.                     {
  341.                         /* begin new DOCUMENT */
  342.                         STRPTR docname = read_string(hp);
  343.                         if (docname)
  344.                         {
  345.                             document = new_document(docname);
  346.                             app_dlnode(hp->documents, (APTR) document);
  347.                             /* free mem allocated by read_string() */
  348.                             ufree(docname);
  349.                         }
  350.                     }
  351.                     else if (!strcmp(cmd, LINE_SOURCE_STR))
  352.                     {
  353.                         /* assign SOURCE */
  354.                         if (document)
  355.                         {
  356.                             STRPTR sourcename = read_string(hp);
  357.                             if (sourcename)
  358.                             {
  359.                                 reallocstr(&(document->sourcename),
  360.                                            sourcename);
  361.                                 /* free mem allocated by read_string() */
  362.                                 ufree(sourcename);
  363.                             }
  364.                         }
  365.                         else
  366.                             hsc_msg_project_corrupt
  367.                                 (hp, LINE_SOURCE_STR " without "
  368.                                  LINE_DOCUMENT_STR);
  369.                     }
  370.                     else if (!strcmp(cmd, LINE_TITLE_STR))
  371.                     {
  372.                         /* assign TITLE */
  373.                         if (document)
  374.                         {
  375.                             STRPTR titlename = read_string(hp);
  376.                             if (titlename)
  377.                             {
  378.                                 set_estr(document->title, titlename);
  379.                                 /* free mem allocated by read_string() */
  380.                                 ufree(titlename);
  381.                             }
  382.                         }
  383.                         else
  384.                             hsc_msg_project_corrupt
  385.                                 (hp, LINE_TITLE_STR " without "
  386.                                  LINE_DOCUMENT_STR);
  387.                     }
  388.                     else if (!strcmp(cmd, LINE_ID_STR))
  389.                     {
  390.                         /* append new ID */
  391.                         if (document)
  392.                         {
  393.                             STRPTR idname = read_string(hp);
  394.                             if (idname)
  395.                             {
  396.                                 HSCIDD *iddef =
  397.                                 app_iddef(document, idname);
  398.                                 iddef->caller = read_caller(hp);
  399.                                 /* free mem allocated by read_string() */
  400.                                 ufree(idname);
  401.                             }
  402.                         }
  403.                         else
  404.                             hsc_msg_project_corrupt
  405.                                 (hp, LINE_ID_STR " without "
  406.                                  LINE_DOCUMENT_STR);
  407.  
  408.                     }
  409.                     else if (!strcmp(cmd, LINE_INCLUDE_STR))
  410.                     {
  411.                         /* store new INCLUDE */
  412.                         if (document)
  413.                         {
  414.                             STRPTR incname = read_string(hp);
  415.                             if (incname)
  416.                             {
  417.                                 HSCINC *inc =
  418.                                 app_include(document, incname);
  419.                                 inc->caller = read_caller(hp);
  420.                                 /* free mem allocated by read_string() */
  421.                                 ufree(incname);
  422.                             }
  423.                         }
  424.                         else
  425.                             hsc_msg_project_corrupt
  426.                                 (hp, LINE_INCLUDE_STR " without "
  427.                                  LINE_DOCUMENT_STR);
  428.                     }
  429.                     else
  430.                     {
  431.                         /* unknown command */
  432.                         hsc_msg_project_corrupt(hp, "unknown tag");
  433.                     }
  434.                 }
  435.                 else
  436.                 {
  437.                     DP(fprintf(stderr, DHP "EOF\n"));
  438.                 }
  439.             }
  440.             while (cmd && !hp->fatal);
  441.  
  442.             ok = !hp->fatal;
  443.         }
  444.         hp->inpf = NULL;
  445.     }
  446.  
  447.     return (ok);
  448. }
  449.  
  450.